from django.shortcuts import render
from django.db.models import Q  # 用于多条件查询
from search.models import Drug
from search.models import Download
from search.models import Data
from django.shortcuts import get_object_or_404


import requests  # 用于连接海洋药物服务器（如果需要）
from django.core.paginator import Paginator

# 首页视图，显示搜索框
def index(request):
    # 第一组数据：类别数组


    # 第一组数据：类别数组
    categories = ["All", "SMILES", "Target", "ID", "Name"]

    # 第二组数据：示例链接
    example_links = [
        {"title": "Aspirin", "path": "/browse/compound/Target/GSPT1"},
        {"title": "EGFR", "path": "/browse/compound/Target/GSPT1"},
        {"title": "C9H8O4", "path": "/browse/compound/Target/GSPT1"},
        {"title": "57-27-2", "path": "/browse/compound/All/CC-220"},
        {"title": " SMILES:C1= C=C(C=C1)C=O", "path": "/browse/compound/ID/123"},
        {"title": "InChI: 1S/C3H6O/c1-3(2)4/h1-2H3",
        "path": "/browse/compound/SMILES/O%3DC1CCC%28N2C%28%3DO%29C3%3DCC%3DCC%3DC3C2%3DO%29C%28%3DO%29N1"},
    ]

    return render(request, 'search/index.html', {
        'categories': categories,
        'example_links': example_links
    })


# 搜索视图，处理搜索请求并返回结果
def search(request):
    query = request.GET.get('query', '')  # 获取用户输入的查询词
    search_results = []

    if query:
        # 如果查询词不为空，使用 Q 对象进行多条件查询
        search_results = Drug.objects.filter(
            Q(name__icontains=query) |  # 名称包含查询词
            Q(formula__icontains=query) |  # 化学式包含查询词
            Q(structure__icontains=query)  # 化学结构式包含查询词
        ).order_by('-name')  # 按名称降序排序

    return render(request, 'search/results.html', {
        'search_results': search_results,
        'query': query  # 将查询内容传递到模板，以便在搜索框中显示
    })

def submit(request):
    return render(request, 'submit.html')
def draw(request):
    return render(request, 'draw.html')


def browse(request):
    # 获取页码参数,默认第1页
    page = request.GET.get('page', 1)
    # 每页显示10条
    page_size = 20

    # 查询所有数据
    download_list = Download.objects.all().order_by('id')

    # 创建分页器
    paginator = Paginator(download_list, page_size)
    # 获取当前页数据
    current_page = paginator.get_page(page)

    context = {
        'page_obj': current_page,
        'download_list': current_page.object_list,
        'total': paginator.count
    }

    return render(request, 'browse.html', context)

def detail(request):
        # 获取页码参数,默认第1页
        id = request.GET.get('id', 0)
        data = get_object_or_404(Data, pk=id)
        context = {
            'data': data
        }

        return render(request, 'detail.html', context)



def data(request):
    return render(request, 'data.html')

# 关于页面视图
def about(request):
    return render(request, 'about.html')

# 文档页面视图
def docs(request):
    return render(request, 'docs.html')

# 提交页面视图
def submit(request):
    return render(request, 'submit.html')

# 联系页面视图
def contact(request):
    return render(request, 'contact.html')




def download(request):
    # 获取页码参数,默认第1页
    page = request.GET.get('page', 1)
    # 每页显示10条
    page_size = 20

    # 查询所有数据
    download_list = Download.objects.all().order_by('id')

    # 创建分页器
    paginator = Paginator(download_list, page_size)
    # 获取当前页数据
    current_page = paginator.get_page(page)

    context = {
        'page_obj': current_page,
        'download_list': current_page.object_list,
        'total': paginator.count
    }

    return render(request, 'download.html', context)